//
//  RecSlideCell.swift
//

import UIKit

class RecSlideCell: UICollectionViewCell {
    
    var items: [RecChildDataItemsModel] = [] {
        didSet {
            collectionView.reloadData()
        }
    }
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let itemWidth = (UIScreen.width - space * 2 - 30.fit) / 3 - 5.fit
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth * 5 / 4 - 0.5)
        layout.minimumLineSpacing = 10.fit
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: space, bottom: 0, right: space)
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.sysTableView
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(RecBigListCell.self)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
           
        addSubviews()
    }
       
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSubviews() {
        backgroundColor = .sysTableView
        contentView.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}

extension RecSlideCell: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withClass: RecBigListCell.self, for: indexPath)
        cell.update(slide: items[indexPath.item])
        return cell
    }
}
